# _*_ coding:utf-8 _*_
# @Time  : 2023.08.24
# @Author: zizlee
import datetime
from fastapi import APIRouter, Depends
from utils.jwt_token import TokenPayload
from response import APIResponse
from db_utils import BaseDatabaseConnection
from .ex_account import ex_account_api
from .ex_stat import ex_stat_api
from .ex_close import ex_close_api
from .mix_evaluate import mix_evaluate_api

ex_doctor_router = APIRouter()
ex_doctor_router.include_router(ex_account_api, prefix='/account')
ex_doctor_router.include_router(ex_stat_api, prefix='/exstat')
ex_doctor_router.include_router(ex_close_api, prefix='/exclose')
ex_doctor_router.include_router(mix_evaluate_api, prefix='/mixEvaluate')


class ExDoctor:
    def __init__(self, user_id: int):
        self.user_id = user_id

    def user_account(self):
        with BaseDatabaseConnection() as conn:
            conn.cursor.execute("SELECT id,cust_num,start_date,end_date FROM ruizhi_user_exchange_doctor WHERE user_id=%s AND is_active=1 LIMIT 1;", [self.user_id])
            acc = conn.cursor.fetchone()
            conn.cursor.execute("SELECT id,is_employee FROM ruizhi_user WHERE id=%s LIMIT 1;", [self.user_id])
            user = conn.cursor.fetchone()

        data = {'cust_num': '', 'start_date': '', 'end_date': '', 'is_test': False}
        if acc:
            data['cust_num'] = acc['cust_num']
            data['start_date'] = acc['start_date'] if acc['start_date'] else (datetime.date.today() - datetime.timedelta(days=90)).strftime('%Y-%m-%d')
            data['end_date'] = acc['end_date'] if acc['end_date'] else datetime.date.today().strftime('%Y-%m-%d')
            data['is_test'] = False
        else:
            if user and user['is_employee']:  # 模拟数据
                data['cust_num'] = '00000000'
                data['start_date'] = '2000-05-01'
                data['end_date'] = '2000-11-30'
                data['is_test'] = True
        return APIResponse.success(data=data)


@ex_doctor_router.get('/')  # 基础数据
def account_exchange_doctor(payload: TokenPayload = Depends(TokenPayload)):
    return ExDoctor(user_id=payload.user_id()).user_account()
